﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace BlueLotus7
{
    partial class Validator
    {
        /// <summary>
        /// Validate whether the string contains Zh charset only.
        /// </summary>
        /// <param name="stringToCheck">The string to be checked.</param>
        /// <param name="minLength">The min length, starts from 0.</param>
        /// <param name="maxLength">The max length, starts from 0.</param>
        /// <returns>The result</returns>
        public static bool IsZhString(string stringToCheck, int minLength, int maxLength)
        {
            if (stringToCheck == null)
                return false;
            Regex reg = new Regex("^([\\u4e00-\\u9fa5]){" + minLength.ToString() + "," + maxLength.ToString() + "}$");
            if (reg.IsMatch(stringToCheck))
                return true;
            return false;
        }

        /// <summary>
        /// Validate whether the string contains Zh,En,Num charset only.
        /// </summary>
        /// <param name="stringToCheck">The string to be checked.</param>
        /// <param name="minLength">The min length, starts from 0.</param>
        /// <param name="maxLength">The max length, starts from 0.</param>
        /// <returns>The result</returns>
        public static bool IsZhEnNumString(string stringToCheck, int minLength, int maxLength)
        {
            if (stringToCheck == null)
                return false;
            Regex reg = new Regex("^(([\\u4e00-\\u9fa5])|([0-9a-zA-Z])){" + minLength.ToString() + "," + maxLength.ToString() + "}$");
            if (reg.IsMatch(stringToCheck))
                return true;
            return false;
        }

        /// <summary>
        /// Validate whether the string contains Zh,En charset only.
        /// </summary>
        /// <param name="stringToCheck">The string to be checked.</param>
        /// <param name="minLength">The min length, starts from 0.</param>
        /// <param name="maxLength">The max length, starts from 0.</param>
        /// <returns>The result</returns>
        public static bool IsZhEnString(string stringToCheck, int minLength, int maxLength)
        {
            if (stringToCheck == null)
                return false;
            Regex reg = new Regex("^(([\\u4e00-\\u9fa5])|([a-zA-Z])){" + minLength.ToString() + "," + maxLength.ToString() + "}$");
            if (reg.IsMatch(stringToCheck))
                return true;
            return false;
        }

        /// <summary>
        /// Validate whether the string contains En,Num charset only.
        /// </summary>
        /// <param name="stringToCheck">The string to be checked.</param>
        /// <param name="minLength">The min length, starts from 0.</param>
        /// <param name="maxLength">The max length, starts from 0.</param>
        /// <returns>The result</returns>
        public static bool IsEnNumString(string stringToCheck, int minLength, int maxLength)
        {
            if (stringToCheck == null)
                return false;
            Regex reg = new Regex("^[0-9A-Za-z]{" + minLength.ToString() + "," + maxLength.ToString() + "}$");
            if (reg.IsMatch(stringToCheck))
                return true;
            return false;
        }

        public static bool ContainsZhCharset(string stringToCheck)
        {
            if (stringToCheck == null)
            {
                return false;
            }
            else
            {
                foreach (char charset in stringToCheck)
                {
                    long charsetCode = Convert.ToInt64(charset);
                    if (charsetCode >= 0x4e00 && charsetCode <= 0x9fa5)
                    {
                        return true;
                    }
                }
                return false;
            }
        }
    }
}
